/**
 * 关于路由的name属性,请与组件的name保持一致,这样cached属性方可生效
 */
import Vue from 'vue'
import Router from 'vue-router'
import constantRoutes from '@/router/constantRoutes'

Vue.use(Router)
// 动态路由列表
let dynamicRoutes = []

/**
 * 遍历引入dynamicRoutes文件夹下的所有路由信息
 */
const files = require.context('./dynamicRoutes', false, /\.js$/)
const requireAll = requireContext => requireContext.keys()
requireAll(files).forEach(key => {
  dynamicRoutes.push(...files(key).default.routes)
})


let router = new Router({
  mode: 'hash',
  routes: []
})

// 防止连续点击多次路由报错
let routerPush = Router.prototype.push;
let routerReplace = Router.prototype.replace;
// push
Router.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(err => err)
}
// replace
Router.prototype.replace = function push(location) {
  return routerReplace.call(this, location).catch(err => err)
}



export {
  // 固定路由
  constantRoutes,
  // 动态的路由
  dynamicRoutes,
}

export default router
